%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Discrimination in Multi-Phase Systems: Evidence from Child Protection 

%Created: 2/8/2023 
%Updated: 6/6/2023 

%This program generates the table summarizing the Hierarchical MTE Model 
%estimates in our paper 

%Note that we have removed the file directory names from this program for confidentiality reasons.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%% SET UP %%%%%%%%
clear all;
rng(42);
path=;
addpath('subroutines/');


% options 
skiplatex=0;

%%%%%% PARAMETER ESTIMATES %%%%%%
param_out=zeros(12,6);
col=4; row=1;
for nosigma=0:0
    for nobeta=1:1
        load(strcat(path,'data/both_nobeta',num2str(nobeta),'_nosigma',num2str(nosigma),'_ests_qje.mat'),'theta_hat_final','theta_hat_init');
        load(strcat(path,'data/both_nobeta',num2str(nobeta),'_nosigma',num2str(nosigma),'_var_qje.mat'),'theta_var');           
        
        %black children
        param_out(row:row+1,col)=[exp(theta_hat_final(1))/(1+exp(theta_hat_final(1)));sqrt(theta_var(1,1))*exp(theta_hat_final(1))/(1+exp(theta_hat_final(1)))^2]; row=row+2;
        param_out(row:row+1,col)=[-theta_hat_final(2);sqrt(theta_var(2,2))]; row=row+2; 
        i=3;
        if nobeta==0
            param_out(row:row+1,col)=[-theta_hat_final(i);sqrt(theta_var(i,i))]; i=i+1; 
        end
        row=row+2; 
        if nosigma==0
            param_out(row:row+1,col)=[exp(theta_hat_final(i));exp(theta_hat_final(i))*sqrt(theta_var(i,i))]; i=i+1; 
        end
        row=row+2; 
        param_out(row:row+1,col)=[theta_hat_final(i);sqrt(theta_var(i,i))]; row=row+2; i=i+1;
        param_out(row:row+1,col)=[exp(theta_hat_final(i));exp(theta_hat_final(i))*sqrt(theta_var(i,i))]; i=i+1;
        row=1; 
        
        %white children
        param_out(row:row+1,col-3)=[exp(theta_hat_final(i))/(1+exp(theta_hat_final(i)));sqrt(theta_var(i,i))*exp(theta_hat_final(i))/(1+exp(theta_hat_final(i)))^2]; row=row+2; i=i+1;
        param_out(row:row+1,col-3)=[-theta_hat_final(i);sqrt(theta_var(i,i))]; row=row+2; i=i+1;
        if nobeta==0
            param_out(row:row+1,col-3)=[-theta_hat_final(i);sqrt(theta_var(i,i))]; i=i+1; 
        end
        row=row+2; 
        if nosigma==0
            param_out(row:row+1,col-3)=[exp(theta_hat_final(i));exp(theta_hat_final(i))*sqrt(theta_var(i,i))]; i=i+1; 
        end
        row=row+2; 
        param_out(row:row+1,col-3)=[theta_hat_final(i);sqrt(theta_var(i,i))]; row=row+2; i=i+1;
        param_out(row:row+1,col-3)=[exp(theta_hat_final(i));exp(theta_hat_final(i))*sqrt(theta_var(i,i))];        
        row=1; col=col+1;
    end
end

temp=param_out(5:8,:);
param_out(5:8,:)=param_out(9:12,:);
param_out(9:10,:)=temp(3:4,:);
param_out(11:12,:)=temp(1:2,:);

csvwrite(strcat(path,'data/both_nobeta',num2str(nobeta),'_nosigma',num2str(nosigma),'_param_out.csv'),param_out)

if skiplatex==0
    % open LaTeX file
    fileID = fopen(strcat(path,'output/table_hierarchical_mte.tex'), 'wt');

    % write table preliminaries
    fprintf(fileID,'\\begin{sidewaystable}[htpb!] \n');
    %fprintf(fileID,'\\begin{table}[htbp!] \n');
    fprintf(fileID,'\\begin{center} \n');
    fprintf(fileID,'\\begin{small} \n');
    fprintf(fileID,'\\caption{Hierarchical MTE Model Estimates} \n');
    fprintf(fileID,'\\label{table_hierarchical_mte} \\medskip');
    fprintf(fileID,'\\begin{tabular}{l ccc c ccc} \n');
    fprintf(fileID,'\\toprule \n');
    fprintf(fileID,'& \\multicolumn{3}{c}{With Monotonicity} && \\multicolumn{3}{c}{Without Monotonicity} \\\\ \n');
    fprintf(fileID,'\\cline{2-4} \\cline{6-8} \n');
    fprintf(fileID,'& \\multicolumn{1}{c}{White} & \\multicolumn{1}{c}{Black} & \\multicolumn{1}{c}{\\multirow{2}{*}{Diff.}} && \\multicolumn{1}{c}{White} & \\multicolumn{1}{c}{Black} & \\multicolumn{1}{c}{\\multirow{2}{*}{Diff.}} \\\\ \n');
    fprintf(fileID,'& \\multicolumn{1}{c}{Children} & \\multicolumn{1}{c}{Children} &  && \\multicolumn{1}{c}{Children} & \\multicolumn{1}{c}{Children} &  \\\\ \n');
    fprintf(fileID,'\\cline{2-4} \\cline{6-8} \n');
    fprintf(fileID,'& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} && \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} \\\\ \n');
    fprintf(fileID,'\\cline{2-8} \n');
end

%%%%%% TRANSLATED PARAMETER ESTIMATEs %%%%%%
out=zeros(12,9);
col=4; row=1;
for nosigma=0:0
    for nobeta=1:1
        load(strcat(path,'data/both_nobeta',num2str(nobeta),'_nosigma',num2str(nosigma),'_ests.mat'),'theta_hat_final','theta_hat_init');
        load(strcat(path,'data/both_nobeta',num2str(nobeta),'_nosigma',num2str(nosigma),'_var.mat'),'theta_var');           
        fs_sims=normrnd(0,1,10000,2);
        rf_sims=normrnd(0,1,10000,2);
        trans=translate_params(theta_hat_final,nobeta,nosigma,fs_sims,rf_sims);
        [~,~,~,~,~,~,G]=lsqnonlin(@(x)translate_params(x,nobeta,nosigma,fs_sims,rf_sims),theta_hat_final,[],[],optimset('MaxIter',0,'TolFun',1000000000));
        trans_var=G*theta_var*G';
        out(row:row+1,col)=[trans(1);sqrt(trans_var(1,1))]; row=row+2;
        out(row:row+1,col)=[trans(5);sqrt(trans_var(5,5))]; row=row+2;
        out(row:row+1,col)=[trans(2);sqrt(trans_var(2,2))]; row=row+2;
        out(row:row+1,col)=[trans(6);sqrt(trans_var(6,6))]; row=row+2;
        out(row:row+1,col)=[trans(3);sqrt(trans_var(3,3))]; row=row+2;
        out(row:row+1,col)=[trans(4);sqrt(trans_var(4,4))]; 
        row=1;
        out(row:row+1,col-3)=[trans(7);sqrt(trans_var(7,7))]; row=row+2;
        out(row:row+1,col-3)=[trans(11);sqrt(trans_var(11,11))]; row=row+2;
        out(row:row+1,col-3)=[trans(8);sqrt(trans_var(8,8))]; row=row+2;
        out(row:row+1,col-3)=[trans(12);sqrt(trans_var(12,12))]; row=row+2;
        out(row:row+1,col-3)=[trans(9);sqrt(trans_var(9,9))]; row=row+2;
        out(row:row+1,col-3)=[trans(10);sqrt(trans_var(10,10))];
        row=1;
        out(row:row+1,col+3)=[trans(13);sqrt(trans_var(13,13))]; row=row+2;
        out(row:row+1,col+3)=[trans(17);sqrt(trans_var(17,17))]; row=row+2;
        out(row:row+1,col+3)=[trans(14);sqrt(trans_var(14,14))]; row=row+2;
        out(row:row+1,col+3)=[trans(18);sqrt(trans_var(18,18))]; row=row+2;
        out(row:row+1,col+3)=[trans(15);sqrt(trans_var(15,15))]; row=row+2;
        out(row:row+1,col+3)=[trans(16);sqrt(trans_var(16,16))];       
        col=col+1; row=1;
    end
end

csvwrite(strcat(path,'data/both_nobeta',num2str(nobeta),'_nosigma',num2str(nosigma),'_out.csv'),out)

% write implied moments out to table
for row=1:2:12
    for col=1:8
        row_se = row+1;

            % implied moments
            b1=out(row,1);
            b2=out(row,2);
            b3=out(row,3);
            b4=out(row,4);
            b5=out(row,5);
            b6=out(row,6);
            b7=out(row,7);
            b8=out(row,8);

            % standard errors
            se1=out(row_se,1);
            se2=out(row_se,2);
            se3=out(row_se,3);
            se4=out(row_se,4);   
            se5=out(row_se,5);
            se6=out(row_se,6);
            se7=out(row_se,7);
            se8=out(row_se,8);

    end
       if skiplatex==0
           % Potential misconduct rate
           if row==1
               fprintf(fileID,'\\hspace{5pt} Mean Risk & %.3f & %.3f & %.3f && %.3f & %.3f & %.3f \\\\ \n', b1, b4, b7, b2, b5, b8);
               fprintf(fileID,' & (%.3f) & (%.3f) & (%.3f) && (%.3f) & (%.3f) & (%.3f) \\\\ \n', se1, se4, se7, se2, se5, se8); 

           % Mean marginal outcome rate 
           elseif row==3
               fprintf(fileID,'\\hspace{5pt} Mean Marginal Outcome & %.3f & %.3f & %.3f && %.3f & %.3f & %.3f \\\\ \n', b1, b4, b7, b2, b5, b8);
               fprintf(fileID,' & (%.3f) & (%.3f) & (%.3f) && (%.3f) & (%.3f) & (%.3f) \\\\ \n', se1, se4, se7, se2, se5, se8); 

           % Mean signal quality   
           elseif row==5
               fprintf(fileID,'\\hspace{5pt} Mean Signal Quality & %.3f & %.3f & %.3f && %.3f & %.3f & %.3f \\\\ \n', b1, b4, b7, b2, b5, b8);
               fprintf(fileID,' & (%.3f) & (%.3f) & (%.3f) && (%.3f) & (%.3f) & (%.3f) \\\\ \n', se1, se4, se7, se2, se5, se8);
              
           % SD marginal outcome rate
           elseif row==7
               fprintf(fileID,'\\hspace{5pt} Marginal Outcome Std. Dev. & %.3f & %.3f & %.3f && %.3f & %.3f & %.3f \\\\ \n', b1, b4, b7, b2, b5, b8);
               fprintf(fileID,' & (%.3f) & (%.3f) & (%.3f) && (%.3f) & (%.3f)  & (%.3f) \\\\ \n', se1, se4, se7, se2, se5, se8);     

           % SD signal quality 
           elseif row==9
               fprintf(fileID,'\\hspace{5pt} Signal Quality Std. Dev. & & & && %.3f & %.3f & %.3f \\\\ \n', b2, b5, b8);
               fprintf(fileID,' & & & && (%.3f) & (%.3f) & (%.3f) \\\\ \n', se2, se5, se8);

           % Cov(signal quality, marginal outcome) 
           elseif row==11
               fprintf(fileID,'\\hspace{5pt} Covariance of Signal Quality and & & & &&  %.3f & %.3f & %.3f \\\\ \n', b2, b5, b8);
               fprintf(fileID,'\\hspace{15pt} Marginal Outcomes & & & && (%.3f) & (%.3f) & (%.3f) \\\\ \n', se2, se5, se8);   
           end
       end
end   

if skiplatex==0
    % lower panel
    fprintf(fileID,'\\hline \n');
    fprintf(fileID,'Investigators & \\multicolumn{1}{c}{699} & \\multicolumn{1}{c}{699} & \\multicolumn{1}{c}{--} && \\multicolumn{1}{c}{699} & \\multicolumn{1}{c}{699} & \\multicolumn{1}{c}{--} \\\\ \n');

    % table closings
    fprintf(fileID,'\\bottomrule \n');
    fprintf(fileID,'\\end{tabular} \n');
    fprintf(fileID,'\\end{small} \n');

    % table notes
    fprintf(fileID,'\\begin{tabular}{ p{\\textwidth} } \n');
    fprintf(fileID,'\\footnotesize\\hspace{1em}{\\emph{Notes.} This table reports simulated minimum distance estimates of moments of the MTE model.');
    fprintf(fileID,' Columns 4-6 estimate the baseline model, while columns 1-3 impose conventional monotonicity.  Robust standard errors, two-way clustered at the child and the investigator level, are obtained by a bootstrapping procedure and appear in parentheses.} \n');
    fprintf(fileID,'\\end{tabular} \n');
    fprintf(fileID,'\\end{center} \n');
    %fprintf(fileID,'\\end{table} \n');
    fprintf(fileID,'\\end{sidewaystable} \n');

    % close LaTeX file
    fclose('all');
end

